package com.travel.dao.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.travel.core.AbstractDao;
import com.travel.dao.IHotelDao;
import com.travel.entities.Hotel;

/**
 * @author SAGITTAR
 */
public class HotelDaoImpl extends AbstractDao implements IHotelDao {

    private static final Logger LOGGER = LogManager.getLogger(HotelDaoImpl.class);

    @Override
    public List<Hotel> selectListOrderBy(String loc, String orderBy, Direction direction) {
        String sql = "SELECT hotel_name hotelName, url hotelUrl, comment_point hotelCommentPoint, comment_score hotelCommentScore, comment_count hotelCommentCount ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("FROM hotel ");
        sqlBuilder.append("WHERE comment_count > 1 ");
        if (StringUtils.isNotBlank(loc)) {
            sqlBuilder.append("AND city = ? ");
        }
        sqlBuilder.append("ORDER BY %s %s LIMIT 0, 10");
        if (Objects.isNull(direction)) {
            direction = Direction.ASC;
        }
        String preparedSql = String.format(sqlBuilder.toString(), orderBy, direction);
        List<Hotel> hotelList = selectListByClassTypeAndSql(Hotel.class, preparedSql, loc);
        LOGGER.info(getLogInfo(true), preparedSql, Arrays.asList(loc));
        LOGGER.info(getResultLog(), hotelList);
        return hotelList;
    }
}
